/* -----------------
* brief
* 1. this is code gen by tools
* 2. mvc code are driver by the model defined with the same filename,
	 for example:
	 there is a Xmodel struct define in xmodel.go file, after the script run (lego model-mvc module_name xmodel)
     it will auto create the code under the module's path
	 m --> xmodel.go
	 v --> view
	 c --> xmodel_ctrl
* 3. you should modify this code if it doesn't satisfy your purpose
*/

package controller

import (
	mchcore "github.com/chanxuehong/wechat/mch/core"
	"github.com/chanxuehong/wechat/mch/pay"
	"github.com/mjiulee/lego/utils"
	"saas_sys_base/common/url_const"
	sysOrders "saas_sys_base/modules/orders/service"
	sysOrdersSvc "saas_sys_base/modules/sysOrders/service"
)

var _WxPayActionCtrl WxPayActionCtrl //全局变量，用于注册到wechat sdk的pay action处理函数回调

type WxPayActionCtrl struct {
	//ServeMsg(*mchcore.Context)
	sysOrdersService sysOrdersSvc.OrdersService
	orderService     sysOrders.OrdersService
}

func init() {
	vc := new(WxPayActionCtrl)
	_ctrlList = append(_ctrlList, vc)
}

func (vc *WxPayActionCtrl) CtrlInfo() string {
	return "WechatPayApiCtrl"
}

func (vc *WxPayActionCtrl) RoutInit() {

}

//Unifiedorder用于小程序支付
func (vc *WxPayActionCtrl) ServeMsg(ctx *mchcore.Context) {
	httpctx := ctx.HttpCtx
	path := httpctx.UserValue(url_const.SUBPATH_LABEL)
	if path == url_const.WX_PAY_TO_SYS_SUBPATH {
		vc.SysPayAction(ctx)
		return
	} else if path == url_const.WX_PAY_TO_TENANT_SUBPATH {
		vc.SysTenantAction(ctx)
	}

}

func (vc *WxPayActionCtrl) SysPayAction(ctx *mchcore.Context) {
	//ctxExt := &lego.RequestCtxExtent{ctx.HttpCtx}
	//fmt.Println("SysPayAction", ctx.Msg)
	payNotifyData := new(pay.PayNotify)

	//mapdecode, _ := mapstructure.NewPathDecoder(&mapstructure.DecoderConfig{TagName: "json", Result:payNotifyData})
	//mapdecode.Decode(ctx.Msg)

	err := utils.Map2Struct(ctx.Msg, payNotifyData)
	if err != nil {
		//ctxExt.ResponseWithStateCode200(1, fmt.Sprintf("消息map解析失败<%s>", err.Error()))
		return
	}
	//mapstructure.Decode( ctx.Msg, payNotifyData)
	//mapstructure.Decode(ctx.Msg, payNotifyData)

	err = vc.sysOrdersService.PayAction(payNotifyData)
	if err != nil {
		//ctxExt.ResponseWithStateCode200(1, fmt.Sprintf("sysOrdersService.PayAction failed<%s>", err.Error()))
		return
	}

	return
}

func (vc *WxPayActionCtrl) SysTenantAction(ctx *mchcore.Context) {
	//ctxExt := &lego.RequestCtxExtent{ctx.HttpCtx}
	//fmt.Println("SysPayAction", ctx.Msg)
	payNotifyData := new(pay.PayNotify)

	//mapdecode, _ := mapstructure.NewPathDecoder(&mapstructure.DecoderConfig{TagName: "json", Result:payNotifyData})
	//mapdecode.Decode(ctx.Msg)

	err := utils.Map2Struct(ctx.Msg, payNotifyData)
	if err != nil {
		//ctxExt.ResponseWithStateCode200(1, fmt.Sprintf("消息map解析失败<%s>", err.Error()))
		return
	}
	//mapstructure.Decode( ctx.Msg, payNotifyData)
	//mapstructure.Decode(ctx.Msg, payNotifyData)

	err = vc.orderService.PayAction(payNotifyData)
	if err != nil {
		//ctxExt.ResponseWithStateCode200(1, fmt.Sprintf("sysOrdersService.PayAction failed<%s>", err.Error()))
		return
	}

	return
}
